package org.liaohailong.victorlibrary.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebViewClient;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.Nullable;
import org.liaohailong.library.widget.nesteddetail.NestedScrollingDetailContainer;
import org.liaohailong.library.widget.nesteddetail.NestedScrollingWebView;
import org.liaohailong.victorlibrary.R;
import org.liaohailong.victorlibrary.base.BaseActivity;
import org.liaohailong.victorlibrary.model.InfoBean;
import org.liaohailong.victorlibrary.ui.adapter.RvAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Victor
 * E-mail: liaohailong190@foxmail.com
 * Date: 2019/6/5 10:37
 * Description:
 * 作者：wangzhengyi
 * GitHub：https://github.com/wangzhengyi/Android-NestedDetail
 * 处理滑动嵌套WebView+不可滑动部分+RecyclerView的解决方案
 * {@link org.liaohailong.library.widget.nesteddetail.NestedScrollingDetailContainer}
 * {@link org.liaohailong.library.widget.nesteddetail.NestedScrollingWebView}
 * <p>
 * / / / / / / / / / / / / / / / /
 * /                             /
 * /                             /
 * /                             /
 * /   NestedScrollingWebView    /
 * /                             /
 * /                             /
 * /                             /
 * / / / / / / / / / / / / / / / /
 * /                             /
 * /         不可滑动区域        /
 * /          （静态布局）       /
 * /                             /
 * / / / / / / / / / / / / / / / /
 * /                             /
 * /                             /
 * /                             /
 * /                             /
 * /         RecyclerView        /
 * /                             /
 * /                             /
 * /                             /
 * / / / / / / / / / / / / / / / /
 */
public class NestedDetailActivity extends BaseActivity {
    public static void show(Context context) {
        Intent intent = new Intent(context, NestedDetailActivity.class);
        context.startActivity(intent);
    }

    private View mToolBar;
    private NestedScrollingDetailContainer mNestedDetailContainer;
    private NestedScrollingWebView mNestedWebView;
    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initImmersed();
        setContentView(R.layout.activity_nested_detail);
        findView();
        initView();
    }

    private void initImmersed() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(option);
            getWindow().setNavigationBarColor(Color.TRANSPARENT);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    private void findView() {
        mToolBar = findViewById(R.id.v_tool_bar);
        mNestedDetailContainer = findViewById(R.id.nested_container);
        mNestedWebView = findViewById(R.id.web_container);
        mRecyclerView = findViewById(R.id.rv_list);
    }

    private void initView() {
        initWebView();
        initRecyclerView();
        initToolBarView();
    }

    private void initToolBarView() {
        if (mToolBar == null) {
            return;
        }
        mToolBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mNestedDetailContainer == null) {
                    return;
                }
                if (mRecyclerView == null) {
                    return;
                }
                mNestedDetailContainer.scrollToTarget(mRecyclerView);
            }
        });
    }

    private void initWebView() {
        if (mNestedDetailContainer == null) {
            return;
        }
        mNestedWebView.getSettings().setJavaScriptEnabled(true);
        mNestedWebView.setWebViewClient(new WebViewClient());
        mNestedWebView.setWebChromeClient(new WebChromeClient());
        mNestedWebView.loadUrl("https://github.com/wangzhengyi/Android-NestedDetail");
    }

    private void initRecyclerView() {
        if (mRecyclerView == null) {
            return;
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        List<InfoBean> data = getCommentData();
        RvAdapter rvAdapter = new RvAdapter(this, data);
        mRecyclerView.setAdapter(rvAdapter);
    }
    private List<InfoBean> getCommentData() {
        List<InfoBean> commentList = new ArrayList<>();
        InfoBean titleBean = new InfoBean();
        titleBean.type = InfoBean.TYPE_TITLE;
        titleBean.title = "评论列表";
        commentList.add(titleBean);
        for (int i = 0; i < 40; i++) {
            InfoBean contentBean = new InfoBean();
            contentBean.type = InfoBean.TYPE_ITEM;
            contentBean.title = "评论标题" + i;
            contentBean.content = "评论内容" + i;
            commentList.add(contentBean);
        }
        return commentList;
    }
}
